package com.wei.btsearch.components;

import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;
import com.wei.btsearch.R;
import com.wei.btsearch.configurations.AppConfiguration;
import com.wei.btsearch.storage.DataBaseOperation;
import com.wei.btsearch.storage.HistoryItem;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class StartSearch extends AppCompatActivity implements HistoryAdaptor.OnItemOperate {
    private static final String TAG = "StartSearch";
    Toolbar toolbar;
    SearchView searchView;
    ListView listView;

    DataBaseOperation dataBaseOperation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        randomSetTheme();
        setContentView(R.layout.activity_start_search);

        initViews();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        } else {
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onStart() {
        setHistory();
        super.onStart();
    }

    private void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        searchView = (SearchView) findViewById(R.id.search);
        listView = (ListView) findViewById(R.id.list);
        dataBaseOperation = new DataBaseOperation(this);

        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                dataBaseOperation.insertContent(query);
                Intent intent = new Intent(StartSearch.this, SearchResult.class);
                intent.putExtra(AppConfiguration.SEARCH_CONTENT, query);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    private void setHistory() {
        Cursor allData = dataBaseOperation.queryAll();
        List<HistoryItem> list = new ArrayList<>();
        while (allData.moveToNext()) {
            list.add(new HistoryItem(allData.getInt(0), allData.getString(1), allData.getLong(2)));
            if (AppConfiguration.DEBUG) {
                Log.d(TAG, new HistoryItem(allData.getInt(0), allData.getString(1), allData.getLong(2)).toString());
            }
        }
        listView.setAdapter(new HistoryAdaptor(this, dataBaseOperation, list, this));
    }


    @Override
    public void onItemDeleted() {
        setHistory();
    }

    private void randomSetTheme() {
        Random random = new Random();
        int type = random.nextInt(6);
        switch (type) {
            case 0:
                setTheme(R.style.AppTheme);
                break;
            case 1:
                setTheme(R.style.BlueTheme);
                break;
            case 2:
                setTheme(R.style.IndegoTheme);
                break;
            case 3:
                setTheme(R.style.LimeTheme);
                break;
            case 4:
                setTheme(R.style.PinkTheme);
                break;
            case 5:
                setTheme(R.style.PunpleTheme);
                break;
            case 6:
                setTheme(R.style.TealTheme);
                break;
        }
    }
}
